package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/2/18 09:48
 * @apiNote
 */

public class M0106CompressString {
    public static void main(String[] args) {
        String s = "a";
        System.out.println(compressString(s));
    }
    public static String compressString(String S) {
        if(S.length() == 0){
            return S;
        }
        char[] chars = S.toCharArray();
        StringBuilder sb = new StringBuilder();
        int count = 1;
        char prev = chars[0];
        for(int i = 1; i < chars.length; i++){
            if(chars[i] == prev){
                count++;
            }else{
                sb.append(prev).append(count);
                prev = chars[i];
                count = 1;
            }
        }
        sb.append(prev).append(count);
        return sb.toString().length() < S.length() ? sb.toString() : S;
    }
}
